<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Toolbars in GTK#</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, GTK#, toolbars, C#, programming, Linux">
<meta name="description" content="In this part of the GTK# tutorial we present toolbars.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content2">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Toolbars in GTK#</h1>

<p>
In this part of the GTK# programming tutorial, we will work with toolbars.
</p>

<p>
Menus group commands that we can use in application. 
Toolbars provide a quick access to the most frequently used commands. A toolbar
is a horizontal or vertical panel with buttons. These buttons have images or
images and text. By clicking on the toolbar button we perform an action. 
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<h2>Simple toolbar</h2>

<p>
Next we create a simple toolbar. 
</p>

<div class="codehead">toolbar.cs</div> 
<pre class="code">
using Gtk;
using System;
 
class SharpApp : Window {
 

    public SharpApp() : base("Toolbar")
    {
        SetDefaultSize(250, 200);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); };
        
        Toolbar toolbar = new Toolbar();
        toolbar.ToolbarStyle = ToolbarStyle.Icons;

        ToolButton newtb = new ToolButton(Stock.New);
        ToolButton opentb = new ToolButton(Stock.Open);
        ToolButton savetb = new ToolButton(Stock.Save);
        SeparatorToolItem sep = new SeparatorToolItem();
        ToolButton quittb = new ToolButton(Stock.Quit);

        toolbar.Insert(newtb, 0);
        toolbar.Insert(opentb, 1);
        toolbar.Insert(savetb, 2);
        toolbar.Insert(sep, 3);
        toolbar.Insert(quittb, 4);

        quittb.Clicked += OnClicked;
         
        VBox vbox = new VBox(false, 2);
        vbox.PackStart(toolbar, false, false, 0);
        
        Add(vbox);

        ShowAll();
    }

    void OnClicked(object sender, EventArgs args)
    {
        Application.Quit();
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}
</pre>

<p>
The example shows a toolbar and four tool buttons. 
</p>

<pre class="explanation">
Toolbar toolbar = new Toolbar();
</pre>

<p>
A <code>Toolbar</code> widget is created. 
</p>

<pre class="explanation">
toolbar.ToolbarStyle = ToolbarStyle.Icons;
</pre>

<p>
On toolbar, we show only icons. No text. 
</p>

<pre class="explanation">
ToolButton newtb = new ToolButton(Stock.New);
</pre>

<p>
A <code>ToolButton</code> with an image from stock is created. 
</p>

<pre class="explanation">
SeparatorToolItem sep = new SeparatorToolItem(); 
</pre>

<p>
This is a separator. It can be used to group toolbar buttons
into logical groups. 
</p>

<pre class="explanation">
toolbar.Insert(newtb, 0);
toolbar.Insert(opentb, 1);
...
</pre>

<p>
Toolbar buttons are inserted into the toolbar widget. 
</p>

<img src="/img/gui/sharpgtk/toolbar.png" alt="Toolbar">
<div class="figure">Figure: Toolbar</div>


<h2>Toolbars</h2>

<p>
In the second example, we show two toolbars. Many applications have 
more than one toolbar. We show, how we can do it in GTK#. 
</p>

<div class="codehead">toolbars.cs</div> 
<pre class="code">
using Gtk;
using System;
 
class SharpApp : Window {
 

    public SharpApp() : base("Toolbars")
    {
        SetDefaultSize(250, 200);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); };
        
        Toolbar upper = new Toolbar();
        upper.ToolbarStyle = ToolbarStyle.Icons;

        ToolButton newtb = new ToolButton(Stock.New);
        ToolButton opentb = new ToolButton(Stock.Open);
        ToolButton savetb = new ToolButton(Stock.Save);

        upper.Insert(newtb, 0);
        upper.Insert(opentb, 1);
        upper.Insert(savetb, 2);

        Toolbar lower = new Toolbar();
        lower.ToolbarStyle = ToolbarStyle.Icons;

        ToolButton quittb = new ToolButton(Stock.Quit);
        quittb.Clicked += OnClicked;
        lower.Insert(quittb, 0);

         
        VBox vbox = new VBox(false, 2);
        vbox.PackStart(upper, false, false, 0);
        vbox.PackStart(lower, false, false, 0);

        Add(vbox);

        ShowAll();
    }

    void OnClicked(object sender, EventArgs args)
    {
        Application.Quit();
    }


    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}
</pre>

<p>
Our applications shows two toolbars. 
</p>

<pre class="explanation">
Toolbar upper = new Toolbar();
...
Toolbar lower = new Toolbar();
</pre>

<p>
We create two <code>Toolbar</code> widgets. 
</p>

<pre class="explanation">
upper.Insert(newtb, 0);
...
lower.Insert(quittb, 0);
</pre>

<p>
Each of them has it's own tool buttons. 
</p>

<pre class="explanation">
VBox vbox = new VBox(false, 2);
vbox.PackStart(upper, false, false, 0);
vbox.PackStart(lower, false, false, 0)
</pre>

<p>
Toolbars are packed into the vertical box, one after the other. 
</p>

<img src="/img/gui/sharpgtk/toolbars.png" alt="Toolbars">
<div class="figure">Figure: Toolbars</div>


<h2>Undo redo</h2>

<p>
The following example demonstrates, how we can inactivate toolbar buttons on the toolbar.
It is a common practise in GUI programming. For example the save button.
If we save all changes of our document to the disk, the save button is inactivated 
in most text editors. This way the application indicates to the user, 
that all changes are already saved. 
</p>

<div class="codehead">undoredo.cs</div>
<pre class="code">
using Gtk;
using System;
 
class SharpApp : Window {
 
    private int count = 2;
    private ToolButton undo;
    private ToolButton redo;

    public SharpApp() : base("Undo redo")
    {
        SetDefaultSize(250, 200);
        SetPosition(WindowPosition.Center);
        DeleteEvent += delegate { Application.Quit(); };
        
        Toolbar toolbar = new Toolbar();
        toolbar.ToolbarStyle = ToolbarStyle.Icons;

        undo = new ToolButton(Stock.Undo);
        redo = new ToolButton(Stock.Redo);
        SeparatorToolItem sep = new SeparatorToolItem();
        ToolButton quit = new ToolButton(Stock.Quit);

        toolbar.Insert(undo, 0);
        toolbar.Insert(redo, 1);
        toolbar.Insert(sep, 2);
        toolbar.Insert(quit, 3);

        undo.Clicked += OnUndo;
        redo.Clicked += OnRedo;
        quit.Clicked += OnClicked;
         
        VBox vbox = new VBox(false, 2);
        vbox.PackStart(toolbar, false, false, 0);
        vbox.PackStart(new Label(), false, false, 0);

        Add(vbox);

        ShowAll();
    }

    void OnUndo(object sender, EventArgs args)
    {
        count -= 1;

        if (count <= 0) {
            undo.Sensitive = false;
            redo.Sensitive = true;
        }
    }

    void OnRedo(object sender, EventArgs args)
    {
        count += 1;

        if (count >= 5) {
            redo.Sensitive = false;
            undo.Sensitive = true;
        }
    }

    void OnClicked(object sender, EventArgs args)
    {
        Application.Quit();
    }

    public static void Main()
    {
        Application.Init();
        new SharpApp();
        Application.Run();
    }
}
</pre>

<p>
Our example creates undo and redo buttons from the GTK# stock resources. 
After several clicks each of the buttons is inactivated. The buttons are grayed out.  
</p>

<pre class="explanation">
private int count = 2;
</pre>

<p>
The count variable decides, which button is activated and
deactivated. 
</p>


<pre class="explanation">
undo = new ToolButton(Stock.Undo);
redo = new ToolButton(Stock.Redo);
</pre>

<p>
We have two tool buttons. Undo and redo tool buttons. Images come from the stock
resources. 
</p>

<pre class="explanation">
undo.Clicked += OnUndo;
redo.Clicked += OnRedo;
</pre>

<p>
We plug a method for the <code>Clicked</code> event for both tool buttons. 
</p>

<pre class="explanation">
if (count &lt;= 0) {
    undo.Sensitive = false;
    redo.Sensitive = true;
}
</pre>

<p>
To activate a widget, we set its <code>Sensitive</code> property to true. 
To inactivate it, we set it to false. 
</p>

<img src="/img/gui/sharpgtk/undoredo.png" alt="Undo redo">
<div class="figure">Figure: Undo redo</div>


<p>
In this chapter of the GTK# programming library, we mentioned toolbars. 
</p>


<div class="center">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>

<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified January 25, 2009  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
